package cc.vv.btpartyjob.homepage.delegate

import android.graphics.Color
import android.text.Spannable
import android.text.SpannableString
import android.text.TextUtils
import android.text.style.ForegroundColorSpan
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import cc.vv.baselibrary.view.ExpandTextview.ExpandTextView
import cc.vv.baselibrary.view.LKAvatarView
import cc.vv.baselibrary.view.LKCircleImageView
import cc.vv.btpartyjob.homepage.R
import cc.vv.lkbasecomponent.util.LKImageUtil
import cc.vv.lkbasecomponent.util.LKScreenUtil
import cc.vv.lkimagecomponent2.LKImage
import cc.vv.lkimagecomponent2.lib.mode.ScaleMode
import cc.vv.mvp.delegate.BaseAppDelegate
import java.util.regex.Pattern

/**
 * 我的主页托管类
 * Created by Chen.qi on 2018/10/24.
 */
class MyHomePageMainFragmentDelegate : BaseAppDelegate() {

    override fun initWidget() {
    }

    override fun initLayoutRes(): Int {
        return R.layout.fragment_main_layout
    }

    /**
     * 设置我的承诺
     */
    fun setIPromise(content: String?) {
        val width = LKScreenUtil.getScreenWidth() - LKScreenUtil.dp2px(23.0f * 2)
        val proText = getView<ExpandTextView>(R.id.etv_txt_content)
        proText?.initWidth(width)
        if (!TextUtils.isEmpty(content)) {
            proText?.setCloseText(content)
        }
    }

    /**
     * 设置头像
     */
    fun setHeadImg(avatar: String?) {
        val headView = getView<LKCircleImageView>(R.id.liv_fml_user_avatar)
        LKImage.load().scale(ScaleMode.CENTER_CROP)
                .placeHolder(R.mipmap.icon_party_head_def)
                .error(R.mipmap.icon_party_head_def).load(avatar).into(headView)
    }

    /**
     * 设置用户名称
     */
    fun setUserName(userName: String?) {
        getView<TextView>(R.id.tv_fml_user_name).text = userName
    }

    /**
     * 设置名称后面的排行
     */
    fun setRankLevel(level: String?) {
        getView<TextView>(R.id.tv_fml_user_rank).text = level
    }

    /**
     * 设置个人签名
     */
    fun setSingelSignature(signature: String?) {
        getView<TextView>(R.id.tv_fml_user_motto).text = signature
    }

    /**
     * 设置个人注册所在地址
     */
    fun setLocationStr(locationStr: String?) {
        getView<TextView>(R.id.tv_fml_user_location).text = locationStr
    }


    /**
     * 累计学习时长
     */
    fun setStudyTimeDuration(str: String?) {
        val study = if (TextUtils.isEmpty(str)) {
            "0分钟"
        } else {
            str
        }
        getView<TextView>(R.id.tv_fml_study_time).text = highlight(study, "[0-9]{1,}", "#FF4E39")
    }


    /**
     * 设置今日学习时间
     */
    fun setTodayStudyTime(todayTime: String?) {
        val view = getView<TextView>(R.id.tv_fml_study_today)
        if (!TextUtils.isEmpty(todayTime)) {
            if (todayTime!!.contains(".")) {
                val split = todayTime.split(".")
                view?.text = split[0]
            } else {
                view?.text = todayTime
            }
        }
    }

    /**
     * 设置学习排名
     */
    fun setStudyRank(studyRank: String?) {
        val view = getView<TextView>(R.id.tv_fml_study_rank)
        if (!TextUtils.isEmpty(studyRank)) {
            if (studyRank!!.contains(".")) {
                val split = studyRank.split(".")
                view?.text = split[0]
            } else {
                view?.text = studyRank
            }
        }
    }

    /**
     * 设置党徽数
     */
    fun setHakenkreuzNum(num: String?) {
        getView<TextView>(R.id.tv_fml_hakenreuz)?.text = num
    }


    /**
     * 关键字高亮显示
     *
     * @param text  需要显示的文字
     * @param target 需要高亮的关键字
     * @param color  高亮颜色
     * @return 处理完后的结果
     */
    private fun highlight(text: String?, target: String, color: String): SpannableString? {
        val spannableString = SpannableString(text)
        val pattern = Pattern.compile(target)
        val matcher = pattern.matcher(text)
        while (matcher.find()) {
            val span = ForegroundColorSpan(Color.parseColor(color))
            spannableString.setSpan(span, matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        }
        return spannableString
    }


    /**
     * 显示隐藏预警项目
     */
    fun showWarningProject(isShow: Boolean) {
        val view = getView<LinearLayout>(R.id.ll_fml_warning)
        val line1 = getView<View>(R.id.vw_fml_showLine1)
        val line2 = getView<View>(R.id.vw_fml_showLine2)

        if (isShow) {
            view?.visibility = View.VISIBLE
            line1?.visibility = View.VISIBLE
            line2?.visibility = View.GONE
        } else {
            view?.visibility = View.GONE
            line1?.visibility = View.GONE
            line2?.visibility = View.VISIBLE
        }


    }

}